import { _decorator, Component, Size, view, sys, Node } from "cc";

/**
 * CreateTime   : 2020年10月21日
 * Des          : 屏幕适配组件
 */
const { ccclass, property } = _decorator;

@ccclass
export class ScreenComponent extends Component {
    private adType: number = 1;

    private designSize: Size;
    private visibleSize = view.getVisibleSize();
    private halfDesignWidth = 0;
    private halfDesignHeight = 0;
    private halfVisibleWidth = 0;
    private halfVisibleHeight = 0;
    private ratioX = 0;
    private ratioY = 0;

    onLoad() {
        let height = 0
        let width = 0
        this.visibleSize = view.getVisibleSize();
        this.designSize = view.getDesignResolutionSize();
        height = view.getVisibleSize().height
        width = view.getVisibleSize().width

        this.halfDesignWidth = this.designSize.width / 2;
        this.halfDesignHeight = this.designSize.height / 2;
        this.halfVisibleWidth = width / 2;
        this.halfVisibleHeight = height / 2;
        this.ratioX = width / this.designSize.width;
        this.ratioY = height / this.designSize.height;
    }

    start() {

        this.screenFitSizeByRatio(this.node);
    }


    // 按比例适配node位置
    screenFitPosByRatio(node) {
        if (!node)
            return;

        node.x = node.x * this.ratioX;
        node.y = node.y * this.ratioY;
    }

    // 按比例适配node位置
    screenFitPosByRatio2(node) {
        if (!node)
            return;

        node.x = node.x * this.ratioX;

        node.y = node.y * this.ratioY;
    }

    // 按比例适配node大小
    screenFitSizeByRatio(node: Node) {
        if (!node)
            return;

        node.wide = node.width * this.ratioX;
        node.height = node.height * this.ratioY;
        node.x = node.x * this.visibleSize.width / this.designSize.width
        node.y = node.y * this.visibleSize.height / this.designSize.height
    }

    // 按比例适配node大小，按像素点适配node位置
    screenFitSizeAndPosByRatioAndPixel(node) {
        if (!node)
            return;

        let oldWidth = node.width;
        let oldHeight = node.height;

        node.width = oldWidth * this.ratioX;
        node.height = oldHeight * this.ratioY;
        node.x = (this.halfDesignWidth + node.x) - this.halfVisibleWidth - (oldWidth - node.width) / 2;
        node.y = (this.halfDesignHeight + node.y) - this.halfVisibleHeight - (oldHeight - node.height) / 2;
    }

    // 按照屏幕宽进行缩放
    screenByWidthRatio(node, scale) {
        if (!node)
            return;

        if (scale && scale.length) {
            node.scaleX = this.getRatioX() * scale[0];
            node.scaleY = this.getRatioY() * scale[1];
        }

        scale = scale || 1;
        node.scale = this.getRatioX() * scale;
    }

    // 按照屏幕高进行缩放
    screenByHeightRatio(node, scale) {
        if (!node)
            return;

        scale = scale || 1;
        node.scale = this.getRatioY() * scale;
    }

    // 根据是否是ipad来调整适配方向
    screenByRatio(node, scale) {
        this.screenByWidthRatio(node, scale);
    }

    getRatioX() {
        return this.ratioX;
    }

    getRatioY() {
        return this.ratioY;
    }

    // 是否是刘海屏
    checkIsFringeScreen() {
        let isFringeScreen = false;
        let frameSize = view.getFrameSize();

        if (frameSize.height / frameSize.width > (16 / 9)) {
            isFringeScreen = true
        }

        return isFringeScreen;
    }

    // 获取刘海屏高度
    getFringeHeight() {
        return 44;
    }

    // 是否是iPhone X
    checkIsIphoneX() {
        let frameSize = view.getFrameSize();
        let isX = (frameSize.width / frameSize.height) == (1125 / 2436);
        let isXMax = (frameSize.width / frameSize.height) == (1242 / 2688);
        let isXr = (frameSize.width / frameSize.height) == (828 / 1792);

        return isX || isXMax || isXr;
    }
}